-------------------------------------------------------------------------------------------------------------------------------------------
local function NPC_TASKS_INIT(inst)
    local theNPC = inst.entity:GetParent()


    theNPC:AddTag("INLIMBO")
    theNPC:AddTag("notarget")
    theNPC:AddTag("noattack")
    theNPC:AddTag("invisible")
    theNPC.npc_event = inst.prefab

end

local function funny_action_fn(theNPC)
    if math.random(100) <= 10 then
        theNPC.AnimState:PlayAnimation("idle_happy")
    end
end

local function face_to_player(theNPC)
    -- local player = TheSim:FindFirstEntityWithTag("player")
    local player = theNPC:GetNearestPlayer()
    if player then
        if theNPC.npc_base_lib:Distance_Targets(player,theNPC) < 4 then
            theNPC.npc_base_lib:FaceToInst(player)
            return true
        end
    end
    return false
end

local function NPC_TASKS(inst)
    local theNPC = inst.entity:GetParent()
    if TheWorld.state.isnight  or TheWorld:HasTag("cave") then
        theNPC.npc_base_lib:Try_2_Set_Light(true)
    else
        theNPC.npc_base_lib:Try_2_Set_Light(false)
    end
    local theplayer = theNPC.__link_player
    if theplayer == nil or theplayer:IsValid() == false then
        -- theNPC:RemoveDebuff(inst.prefab)
        inst:Remove()
        theNPC:AddDebuff("npc_event_leave","npc_event_leave")
        print("Warning : theNPC.__link_player == nil")
        return
    end

    if theNPC.event_step == nil then
        theNPC.npc_base_lib:Run_Around_Target_In_Range({range = 10,random_range_flag = true,walk = true ,fn = function(theNPC) theNPC.event_step = 1 end  })        
        if not TheWorld:HasTag("cave") and TheWorld.state.isnight then
            theNPC.event_step = 10
        end
        theNPC.npc_everything_data:Set("time",0)
        theNPC.npc_everything_data:Set("time random",math.random(5,10))

        if theNPC.npc_base_lib:StackCheck(2,30) then    --- 如果被卡住了
            theNPC.npc_base_lib:Run_Around_Target_In_Range({clean = true})
            theNPC.event_step = 2
        end

    elseif theNPC.event_step == 1 then
        local time = theNPC.npc_everything_data:Add("time",1)
        if time == 1 then
            funny_action_fn(theNPC)
        end

        if face_to_player(theNPC) == false and time > theNPC.npc_everything_data:Get("time random") then
            theNPC.event_step = nil
        end        
    elseif theNPC.event_step == 2 then --- 卡住了
        if theNPC.npc_base_lib:Close_2_Target_In_Range({range = 2.65,walk = true}) == true then
            theNPC.event_step = nil
        end
    elseif theNPC.event_step == 10 then
        if theNPC.npc_base_lib:Close_2_Target_In_Range({range = 2.65,walk = true}) == true then
            theNPC.npc_base_lib:Run_Around_Target_In_Range({clean = true})
            -- theNPC.event_step = 1
            theplayer.SoundEmitter:PlaySound("dontstarve/common/pighouse_door")
            theNPC:Remove()
        end
    end
end
--------------------------------------------------------------------------------------------------------------------------------------------------

local function OnAttached(inst,target) -- when the debuff on or player is newSpawn(or through the cave)
    inst.entity:SetParent(target.entity)
    inst.Network:SetClassifiedTarget(target)
    local NPC = inst.entity:GetParent()
    local flag ,error_code= pcall(function(inst,target)
        if TheWorld.ismastersim then

            NPC_TASKS_INIT(inst)


        end
    end,inst,target)
    if flag == false  then
        print("Error : OnAttached(inst,target) ",inst.prefab)
        print(error_code)
        NPC:Remove()
    end
end

local function OnDetached(inst) -- When eliminating from the outside
    local player = inst.entity:GetParent()   
    -- print(" FROM OUSIDE REMOVE ")
    
    -- inst:Remove()
end

local function ExtendDebuff(inst)
    -- inst.countdown = 3 + (inst._level:value() < CONTROL_LEVEL and EXTEND_TICKS or math.floor(TUNING.STALKER_MINDCONTROL_DURATION / FRAMES + .5))
end



local function OnUpdate(inst,ismastersim)
    local NPC = inst.entity:GetParent()
    local flag,error_code = pcall(function(inst,ismastersim)
        -- local NPC = inst.entity:GetParent()

        NPC_TASKS(inst)


    end,inst,ismastersim)
    if flag == false then
        print("Error : OnUpdate(inst,ismastersim) ,step:",NPC.event_step ,inst.prefab)
        print(error_code)
        NPC:Remove()
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()

    inst:AddTag("CLASSIFIED")


    -- inst:DoPeriodicTask(1, OnUpdate, nil, TheWorld.ismastersim)  -- in one seconds
    inst:DoPeriodicTask( 1, OnUpdate, nil, TheWorld.ismastersim)  -- in 0.5 seconds

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)
    inst.components.debuff.keepondespawn = false -- 是否保持debuff 到下次登陆
    -- inst.components.debuff:SetDetachedFn(inst.Remove)
    inst.components.debuff:SetDetachedFn(OnDetached)
    -- inst.components.debuff:SetExtendedFn(ExtendDebuff)
    -- ExtendDebuff(inst)


    return inst
end

return Prefab("npc_event_walrus_camp", fn)
